package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.MonitorEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by xu_jiaHui on 2017/8/8.
 */
public interface MonitorRepository extends CrudRepository<MonitorEntity, Integer> {

    /**
     * @Description: 监控箱产品DT展示
     * @author xu_jiaHui
     * @date 2017/8/8 15:15
     */
    public List<MonitorDto> getMonitor4DT(DTRequestDto dtRequestDto,
                                          String actorIds,
                                          Integer orgId,
                                          Boolean audit,
                                          String startRoad,
                                          String endRoad,
                                          String roadName,
                                          Integer alarmBinding) throws Exception;

    /**
     * @Description: 监控箱产品DT展示
     * @author xu_jiaHui
     * @date 2017/8/8 15:30
     */
    public Integer getMonitor4DTCount(DTRequestDto dtRequestDto,
                                      String actorIds,
                                      Integer orgId,
                                      Boolean audit,
                                      String startRoad,
                                      String endRoad,
                                      String roadName,
                                      Integer alarmBinding) throws Exception;

    /**
     * @Description: 通过ID删除监控箱
     * @author xu_jiaHui
     * @date 2017/8/8 20:47
     */
    @Modifying
    @Query(value = "delete from MonitorEntity  where monitorId=?1")
    public Integer deleteMonitor(int monitorId) throws Exception;

    /**
     * @Description: 更新监控箱信息
     * @author xu_jiaHui
     * @date 2017/8/9 12:36
     */
    @Modifying
    @Query(value = "update MonitorEntity set projectNumber=?2, monitorLongitude=?3,monitorLatitude=?4,  monitorProvince=?5, " +
            " monitorCity=?6, monitorArea=?7,monitorRoadName=?8,monitorStartRoad=?9,monitorEndRoad=?10,monitorDirection=?11," +
            "monitorPoleShape=?12,monitorPoleSize=?13,monitorBrand=?14,monitorPosition=?15,monitorSize=?16,monitorPhoto=?17," +
            "monitorInstallTime=?18,monitorInstallOrg=?19,monitorPeriod=?20,monitorServiceLife=?21,monitorRemark=?22,monitorCreatorId=?23," +
            "monitorModifierId=?24,monitorOrgId=?25,monitorMainOrgId=?26,monitorIsComplete=0 where monitorId=?1")
    public Integer updateMonitor(Integer monitorId,
                                 String projectNumber,
                                 String monitorLongitude,
                                 String monitorLatitude,
                                 String monitorProvince,
                                 String monitorCity,
                                 String monitorArea,
                                 String monitorRoadName,
                                 String monitorStartRoad,
                                 String monitorEndRoad,
                                 Integer monitorDirection,
                                 Integer monitorPoleShape,
                                 String monitorPoleSize,
                                 String monitorBrand,
                                 Integer monitorPosition,
                                 String monitorSize,
                                 String monitorPhoto,
                                 String monitorInstallTime,
                                 String monitorInstallOrg,
                                 Integer monitorPeriod,
                                 Integer monitorServiceLife,
                                 String monitorRemark,
                                 Integer monitorCreatorId,
                                 Integer monitorModifierId,
                                 Integer monitorOrgId,
                                 Integer monitorMainOrgId) throws Exception;

    /**
     * @Description: 根据路名获取起始路段
     * @author xu_jiaHui
     * @date 2017/8/9 13:13
     */
    public List<String> getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception;

    /**
     * @Description: 根据所在路名 起始路名获取结束路名
     * @author xu_jiaHui
     * @date 2017/8/9 13:27
     */
    public List<String> getEndRoadByStartRoad(String city, String monitorRoadName, String startRoad, String actorIds, Integer orgId) throws Exception;

    /**
     * @Description: 获取监控箱信息用于列表展示4App
     * @author xu_jiaHui
     * @date 2017/8/9 14:09
     */
    public List<MonitorDto> getMonitor4APP(String actorIds,
                                           Integer orgId,
                                           Integer mCount,
                                           Integer mSize,
                                           String city,
                                           String startRoad,
                                           String endRoad,
                                           String roadName,
                                           String alarmType) throws Exception;


    /**
     * @Description: 获取监控箱坐标4MAP
     * @Author: pan-jianan
     * @date 2017/8/3 14:11
     */
    public List<MonitorFaultDto> getMonitorCoordinate4Map(Integer orgId, Integer actorId) throws Exception;

    /**
     * @Description: 根据坐标获取监控箱信息
     * @Author: pan-jianan
     * @date 2017/8/3 14:11
     */
    @Query(value = "select new com.hualude.roadsign.dto.MonitorFaultDto(m.monitorId,m.monitorOrgId,m.monitorMainOrgId," +
            "m.monitorLongitude,m.monitorLatitude,m.monitorProvince,m.monitorCity,m.monitorArea,m.monitorRoadName," +
            "m.monitorStartRoad,m.monitorEndRoad,m.monitorDirection,m.monitorPoleShape,m.monitorPoleSize,m.monitorBrand," +
            "m.monitorPosition,m.monitorSize,m.monitorPhoto,m.monitorCreatorId) " +
            "from MonitorEntity m where m.monitorLongitude=?1 and m.monitorLatitude=?2")
    public List<MonitorFaultDto> getMonitorEnergyByXY(String longitude, String latitude) throws Exception;

    public List<DTMonitorFaultDto> getMonitorFaultReport4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId, String roadName, String startRoad, String endRoad) throws Exception;

    /**
     * @Description: 根据Id获取信号机信息
     * @Author: xu_jiaHui
     * @Date: 2017/8/10 16:38
     */
    public List<MonitorDto> getMonitorById(Integer monitorId) throws Exception;

    /**
     * @Description: 获取信号机城市列表4App
     * @Author: xu_jiaHui
     * @Date: 2017/8/10 16:46
     */
    public List<String> getCity4Monitor4App(String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 通过city获取路名列表
     * @Author: xu_jiaHui
     * @Date: 2017/8/10 16:52
     */
    public List<String> getRoadName4MonitorByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 根据 城市 路名 起始路名 获取结束路名
     * @Author: xu_jiaHui
     * @Date: 2017/8/11 19:59
     */
    public List<String> getEndRoadByStartRoad4App(String city, String monitorRoadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize);

    /**
     * @Description: 根据 城市 路名 获取起始路名
     * @Author: xu_jiaHui
     * @Date: 2017/8/11 20:51
     */
    public List<String> getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception;

    /**
     * @Description: 获取监控箱信息用于app端地图展示
     * @Author: xu_jiaHui
     * @Date: 2017/8/11 22:36
     */
    public List<MonitorDto> getMonitor4Map4App(Integer orgId,
                                               String actorIds,
                                               double neLongitude,
                                               double neLatitude,
                                               double swLongitude,
                                               double swLatitude) throws Exception;

    /**
     * @Description: 审核监控箱信息
     * @Author: xu_jiaHui
     * @Date: 2017/8/12 19:10
     */
    @Modifying
    @Query(value = "update MonitorEntity set monitorIsComplete=1 where monitorId=?1")
    public Integer auditMonitor(Integer monitorId) throws Exception;

    /**
     * @Description: 获取监控箱坐标用于地图显示
     * @Author: xu_jiaHui
     * @Date: 2017/8/13 19:48
     */
    public List<MonitorDto> getMonitorCoordinates4Map(Integer orgId, Integer orgType, Integer actorId) throws Exception;

    /**
     * @Description: 通过坐标获取监控箱信息
     * @Author: xu_jiaHui
     * @Date: 2017/8/13 21:11
     */
    @Query(value = "select new com.hualude.roadsign.dto.MapInfoDto(sm.monitorPhoto,sm.monitorId,sm.projectNumber," +
            "sm.monitorProvince,sm.monitorCity,sm.monitorArea,sm.monitorRoadName,sm.monitorStartRoad," +
            "sm.monitorEndRoad,sm.monitorSize,sm.monitorBrand,sm.monitorInstallTime,sm.monitorInstallOrg,sm.monitorPeriod," +
            "sm.monitorServiceLife,sm.monitorRemark,u.userName,o1.orgName,o2.orgName,sm.monitorDirection,sm.monitorPoleSize," +
            "sm.monitorPosition,sm.monitorPoleShape) from MonitorEntity sm,OrgEntity o1,OrgEntity o2,UserEntity u where " +
            "o1.orgId=sm.monitorOrgId and o2.orgId=sm.monitorMainOrgId and sm.monitorCreatorId=u.userId and sm.monitorLongitude=?1 " +
            "and sm.monitorLatitude=?2")
    public MapInfoDto getMonitorInfoByXY(String longitude, String latitude) throws Exception;

    /**
     * Created by WYX on 2018/4/29.
     */

    @Modifying
    @Query(value = "update MonitorEntity set alarmDeviceId=?1 where monitorId=?2 and alarmDeviceId=null")
    public Integer bindingDevice(String deviceId, Integer monitorId) throws Exception;

    @Modifying
    @Query(value = "update MonitorEntity set alarmDeviceId=null where monitorId=?1")
    public Integer unbindingDevice(Integer monitorId) throws Exception;

    public List<MonitorDto> getDeviceInfo(Integer deviceId) throws Exception;


}
